﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace view
{
    public class HttpModule : IHttpModule, IRequiresSessionState
    {

        public void Dispose()
        {
            throw new NotImplementedException();
        }

        public void Init(HttpApplication context)
        {
            context.PreRequestHandlerExecute += new EventHandler(IsLogin);
        }

        /// <summary>
        /// 判断是否已经登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void IsLogin(object sender, EventArgs e)
        {
            HttpCookie cookieLoginCookie = HttpContext.Current.Request.Cookies["loginCookie"];

            if (cookieLoginCookie != null)
            {
                JU.MODEL.Login login = new JU.MODEL.Login()
                {
                    LoginName = HttpContext.Current.Request.Cookies["loginName"].Value,
                    LoginCookie = cookieLoginCookie.Value
                };
                if (JU.BLL.LoginManager.IsLogin(login))
                {
                    JU.BLL.LoginManager.GetUserSessionByLoginName(login.LoginName);
                }
                else
                {
                    HttpCookie newCookieLoginCookie = new HttpCookie("loginCookie", "");
                    newCookieLoginCookie.Expires = DateTime.Now.AddMinutes(-1);
                    HttpContext.Current.Response.Cookies.Add(newCookieLoginCookie);
                    HttpCookie newCookieLoginName = new HttpCookie("loginName", "");
                    cookieLoginCookie.Expires = DateTime.Now.AddMinutes(-1);
                    HttpContext.Current.Response.Cookies.Add(cookieLoginCookie);
                }
            }
        }
    }
}